package com.example.myapplication.net.usecase;

import com.example.myapplication.net.exception.NewAccountException;
import com.example.myapplication.net.repository.IMainRepository;
import com.example.myapplication.net.repository.MainRepository;
import com.example.common.base.INetCallBack;
import com.example.common.base.UseCase;
import ohos.app.Context;

/**
 * @author geyao
 * @date 2021/7/6
 * des:
 */
public class GetPhoneVerifyCodeUseCase extends UseCase<Boolean> {
    private final String phoneNum;
    private final IMainRepository repository;

    public GetPhoneVerifyCodeUseCase(Context context, boolean isShowLoading, String phoneNum) {
        super(context, isShowLoading);
        this.phoneNum = phoneNum;
        this.repository = new MainRepository(context);
    }

    @Override
    public void buildWork(INetCallBack<Boolean> callBack) {
        try {
            //判断是否是新用户
            boolean newAccount = repository.isNewAccount(phoneNum);
            if (newAccount) {
                throw new NewAccountException();
            }
            //非新用户发送验证码
            boolean sendResult = repository.sendPhoneVerifyNum(phoneNum);
            callBack.onNext(sendResult);

        } catch (Exception e) {
            e.printStackTrace();
            callBack.onError(e);
        }
    }
}
